/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package per.wsj.andratingbar.ui;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.app.Context;
import per.wsj.andratingbar.ResourceTable;
import per.wsj.library.AndRatingBar;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * SpacingFragment
 *
 * @since 2021-04-13
 */
public class SpacingFragment extends Fraction {
    private Slider seekBar1;
    private Slider seekBar2;

    private AndRatingBar ratingBar1;
    private AndRatingBar ratingBar2;

    private Text tvScaleFactor;
    private Text tvSpacing;

    private Context mContext;

    /**
     * SpacingFragment
     *
     * @param context 上下文
     */
    public SpacingFragment(Context context) {
        this.mContext = context;
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_fragment_spacing,
                container, false);
        seekBar1 = (Slider) component.findComponentById(ResourceTable.Id_seekBar1);
        seekBar2 = (Slider) component.findComponentById(ResourceTable.Id_seekBar2);
        ratingBar1 = (AndRatingBar) component.findComponentById(ResourceTable.Id_ratingBar1);
        ratingBar2 = (AndRatingBar) component.findComponentById(ResourceTable.Id_ratingBar2);
        tvScaleFactor = (Text) component.findComponentById(ResourceTable.Id_tvScaleFactor);
        tvSpacing = (Text) component.findComponentById(ResourceTable.Id_tvSpacing);
        return component;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        seekBar2.setDividerLineColor(Color.BLACK);
        seekBar1.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean isFromUser) {
                float factor = formatNum(0.01 * progress + 0.8);
                tvScaleFactor.setText("current factor " + factor);
                ratingBar1.setScaleFactor(factor);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });

        seekBar2.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean isFromUser) {
                float spacing = progress - 15;
                ratingBar2.setStarSpacing(dip2px(mContext, spacing));
                tvSpacing.setText("current extra spacing:" + spacing + " vp");
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
    }

    private float formatNum(double value) {
        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(2, RoundingMode.HALF_UP);
        return bd.floatValue();
    }

    private int dip2px(Context context, float dpVal) {
        return AttrHelper.vp2px(dpVal, context);
    }
}